﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using DevTrends.MvcDonutCaching;
using KMF.Common;
using KMF.DAL;
using KMF.Utility;
using KMF.WebUI.CustomHelpers;
using KMF.WebUI.Infrastructure;
using System.Security.Cryptography;
using KMF.WebUI.Models;

namespace KMF.WebUI.Controllers
{
    public class CategoryL1Controller : Controller
    {
        //
        // GET: /CategoryL1/

        public ActionResult Index(string cate = "3",string cateName="")
        {
            ViewBag.CateId = cate;
            var model = ProductCategoryDb.GetChildCate(cate) ?? new List<ProductChildCateDto>();
            return View(model);
        }
        public ActionResult MenuLeft(string cateId = "")
        {
            ViewBag.pCate = cateId;
            var model = ProductCategoryDb.GetMenuLeftOnCategoryHome() ?? new List<ProductCategoryMenuLeftDto>();
            return PartialView(model);
        }


        public ActionResult ProductByCateL2(string cateName = "", string cateId = "", string catefil = "OEJHY")
        {
            var itemCate = ProductCategoryDb.GetById(cateId) ?? new ProductCategoryDto();
            ViewBag.listCateMapLink = ProductCategoryMapLinkDb.ShowBYCate(cateId);
            var str = string.Empty;
            ViewBag.CateFill = catefil;
            ViewBag.CateName = cateName;
            return View(itemCate);
        }
        public ActionResult ProductByCateFilter(string cateName = "", string cateId = "", string catefil = "OEJHY")
        {
            var totalRecord = 0;
            var model = new List<ProductRateByCateTopListPagingDto>();
            if (catefil == "OEJHY")
            {
                // cac san pham moi nhat
                model = ProductDb.GetTopNewProduct(ref totalRecord, cateId) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "ACCEH")
            {
                // cac san pham khuyen mai
                model = ProductDb.GetListDiscountProduct(ref totalRecord, cateId) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "GJUYR" || catefil == "FILL") //tat ca sp
            {
                //tat ca san pham
                model = ProductDb.GetListAllProduct(ref totalRecord, cateId) ?? new List<ProductRateByCateTopListPagingDto>();

            }

            ViewBag.Category = cateId;
            //ViewBag.Fillter = sortdrop;
            ViewBag.CateName = cateName;
            ViewBag.CateFill = catefil;
            ViewBag.TotalRecod = totalRecord;
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult ProductByCateFilter(ProductOrderInfo pInfo)
        {
            if (Session[SessionConfig.SessionCartInfor] == null)
            {
                var cartInfor = new OrderInfo { ProductOrderList = new List<ProductOrderInfo> { pInfo } };
                cartInfor.TotalMoney = pInfo.PromotionPrice > 0 ? pInfo.PromotionPrice * pInfo.ProductQuantity : pInfo.RootPriceProduct * pInfo.ProductQuantity;
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }
            else
            {
                var cartInfor = (OrderInfo)Session[SessionConfig.SessionCartInfor];
                cartInfor = FunctionHelpers.CalTotalOrder(cartInfor, pInfo);
                Session[SessionConfig.SessionCartInfor] = cartInfor;
            }

            // Check login
            return RedirectToAction("Finish", "Order");
        }
        public PartialViewResult TabProductByCateFilter(string categoryId = "", string catefil = "OEJHY")
        {
            var totalRecord = 0;
            var model = new List<ProductRateByCateTopListPagingDto>();
            if (catefil == "OEJHY")
            {
                // cac san pham moi nhat
                model = ProductDb.GetTopNewProduct(ref totalRecord, categoryId) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "ACCEH")
            {
                // cac san pham khuyen mai
                model = ProductDb.GetListDiscountProduct(ref totalRecord, categoryId) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "GJUYR" || catefil == "FILL") //tat ca sp
            {
                //tat ca san pham
                model = ProductDb.GetListAllProduct(ref totalRecord, categoryId) ?? new List<ProductRateByCateTopListPagingDto>();

            }

            ViewBag.Category = categoryId;

            //ViewBag.Fillter = sortdrop;
            ViewBag.CateFill = catefil;
            ViewBag.TotalRecod = totalRecord;
            return PartialView(model);
        }
    }
}
